package com.example.gaojt.androiddemo;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;


import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by lakinqian on 2016-11-1.
 */
public class ParamsUtil {
    public final static String TAG = "ParamsUtil";


    /**
     * 获取imei号
     *
     * @return
     */

    public static String getIMEI(Context context) {
        try {
            if (Build.VERSION.SDK_INT < 23) {
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                return telephonyManager.getDeviceId();
            } else {
                return getImeiByMiddleMan(context);
            }
        } catch (Exception e) {

        }
        return "";
    }

    private static String getImeiByMiddleMan(Context context) {
        String imeiStr = "";
        Cursor cursor = null;
        try {
            final Uri uri = Uri.parse("content://com.tct.gapp.middleman" + "/" + context.getPackageName());
            cursor = context.getContentResolver().query(uri, null, "getDeviceID", null, null);
            if (cursor != null) {
                Bundle bundle = cursor.getExtras();
                if (bundle.getInt("result_code") == 1) {
                    imeiStr = bundle.getString("result");
                } else {
                    if (bundle.getInt("result_code") == 2) {
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return imeiStr;
    }

    /**
     * 获取国家语言
     *
     * @return
     */
    public static String getLanguage() {
        Locale locale = Locale.getDefault();
        return locale.getLanguage() + "_" + locale.getCountry();
    }

    /**
     * 获取国家代码
     *
     * @return
     */
    public static String getCountryCode() {
        return Locale.getDefault().getCountry();
    }

    /**
     * 获取手机型号
     */
    public static String getMobileVersion() {
        return Build.MODEL;
    }

    /**
     * 获取系统版本
     */
    public static String getAndroidVersion() {
        return Build.VERSION.RELEASE;
    }

    /**
     * 获取应用版本号
     */
    public static String getVersionName(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取Android API版本代码，比如10
     */
    public static String getAndroidApiVersion() {
        String osVersionCode = Build.VERSION.SDK_INT + "";
        return osVersionCode;

    }

    /**
     * 获取网络类型
     */
    public static String netWork(Context context) {
        ConnectivityManager mConnectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = mConnectivity.getActiveNetworkInfo();
        if (info == null || !mConnectivity.getBackgroundDataSetting()) {
            return "noNetWork";
        } else {
            int netType = info.getType();
            if (netType == ConnectivityManager.TYPE_WIFI) {  //WIFI
                return "wifi";
            } else if (netType == ConnectivityManager.TYPE_MOBILE) {   //MOBILE
                return "mobile";
            } else {
                return "";
            }
        }
    }

    /**
     * 判断请求来自手机还是平板
     */
    public static String isTablet(Context context) {
        boolean boo = (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
        if (boo) {
            return "isTablet";
        } else {
            return "isMobile";
        }
    }

    /**
     * 获取手机所在时区
     */
    public static String getTimeZone() {
        TimeZone tz = TimeZone.getDefault();
        String timeZone = "TimeZone:" + tz.getDisplayName(false, TimeZone.SHORT) + " / Timezon id :: " + tz.getID();
        return timeZone;
    }

    /**
     * 获取手机的Android_ID
     *
     * @return
     */
    public static String getAndroidId(Context context) {
        return Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    /**
     * 获取手机MAC地址
     *
     * @return
     */
    public static String getMacAddress() {
        String macAddress = null;
        String str;
        try {
            Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            if ((str = input.readLine()) != null) {
                macAddress = str.trim(); //去空格
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            macAddress = "null"; //wifi未开启，读取不到mac的值，用null代替
        }
        return macAddress;
    }
}
